package com.iyue.servlet.taglibs;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.iyue.Config;
import com.iyue.SpringHelper;
import com.iyue.framework.secu.Compress64;
import com.iyue.sys.pojo.SysCity;
import com.iyue.sys.pojo.SysPhoneArea;
import com.iyue.sys.pojo.SysProvince;
import com.iyue.sys.service.SysBiz;
import com.iyue.util.ValidCode;
import com.iyue.util.face.Face;

/**
 *
 *
 * @author fresh
 * @version $Id: Functions.java,v 1.7 2011/01/05 09:36:11 fresh Exp $
 */
public final class Functions extends FunctionsUtil {

	// 密码验证失败次数
	public static final String KEY_PASSWORD_FAIL = "com.iyue.portal.pojo.passwordfail_mobile_";
	// 修改密码的验证码
	public static final String KEY_PASSWORD_VCODE = "com.iyue.portal.pojo.passwordvcode_mobile_";

	/**
	 * 图片显示
	 *
	 * @param src
	 *            相对地址
	 * @return String
	 */
	public static String img(String src) {

		Config cfg = (Config) SpringHelper.getBean("test");

		StringBuffer sb = new StringBuffer();
		sb.append("<img src=\"");
		sb.append(cfg.getImg());
		sb.append("/images/");
		sb.append(src);
		sb.append("\" alt=\".\" />");

		return sb.toString();
	}

	/**
	 * 图片显示
	 *
	 * @param src
	 *            相对地址
	 * @return String
	 */
	public static String pic(String src, String resize, String width,
			String height) {

		Image1Tag tag = new Image1Tag();

		tag.setSrc(src);

		tag.setWidth(width);
		tag.setHeight(height);

		StringBuffer sb = tag.src(resize);

		return sb.toString();
	}

	/**
	 *
	 */
	public static String wml(String s) {
		String a = txt(s);

		return a.replaceAll("\r\n|\n\r|\n|\r", "<br/>");
	}

	public static String encode(String s) {
		if (s == null) {
			return "";
		}
		try {
			return URLEncoder.encode(s, "UTF-8");
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return "";
	}

	public static String decode(String s) {
		if (s == null) {
			return null;
		}

		try {
			return URLDecoder.decode(s, "UTF-8");
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return null;
	}

	public static String clearUrl(String content) {

		String regEx = "(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?";
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		return m.replaceAll("***");
	}

	public static String age(Date date) {
		if (date == null) {
			return "";
		}

		Calendar birthday = Calendar.getInstance();
		birthday.setTime(date);

		Calendar today = Calendar.getInstance();
		int age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
		birthday.add(Calendar.YEAR, age);
		if (birthday.after(today)) {
			age--;
		}

		return String.valueOf(age);
	}

	public static String age(String s) {
		if (s == null) {
			return "";
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date = null;
		try {
			date = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}

		Calendar birthday = Calendar.getInstance();
		birthday.setTime(date);

		Calendar today = Calendar.getInstance();
		int age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
		birthday.add(Calendar.YEAR, age);
		if (birthday.after(today)) {
			age--;
		}

		return String.valueOf(age);
	}

	@SuppressWarnings("static-access")
	public static String duration(Date date) {
		if (date == null) {
			return "";
		}

		Calendar before = Calendar.getInstance();
		before.setTime(date);

		Calendar today = Calendar.getInstance();
		long durationMillis = today.getTimeInMillis()
				- before.getTimeInMillis();

		StringBuffer sb = new StringBuffer();

		int days = 0;
		if (durationMillis / DateUtils.MILLIS_PER_DAY > 0) {
			days = (int) (durationMillis / DateUtils.MILLIS_PER_DAY);
			if (days > 30) {
				SimpleDateFormat sdf;
				if (today.get(today.YEAR) > before.get(before.YEAR)) {
					sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				} else {
					sdf = new SimpleDateFormat("MM-dd HH:mm");
				}
				sb.append(sdf.format(before.getTime()));
				return sb.toString();
			}
			sb.append(days);
			sb.append("天前");
			return sb.toString();
		}

		int hours = 0;
		if (durationMillis / DateUtils.MILLIS_PER_HOUR > 0) {
			hours = (int) (durationMillis / DateUtils.MILLIS_PER_HOUR);
			sb.append(hours);
			sb.append("小时前");
			return sb.toString();
		}

		int minutes = 0;
		if (durationMillis / DateUtils.MILLIS_PER_MINUTE > 0) {
			minutes = (int) (durationMillis / DateUtils.MILLIS_PER_MINUTE);
			sb.append(minutes);
			sb.append("分钟前");
			return sb.toString();
		}

		int seconds = 0;
		if (durationMillis / DateUtils.MILLIS_PER_SECOND >= 0) {
			seconds = (int) (durationMillis / DateUtils.MILLIS_PER_SECOND);
			sb.append(seconds);
			sb.append("秒前");
			return sb.toString();
		}

		return "1秒前";
	}

	public static String duration2(String dateStr) {
		try {
			Date date = null;
			if (dateStr.matches("[0-9]+")) {
				date = new Date(Long.parseLong(dateStr));
			} else if (dateStr
					.matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")) {
				date = DateUtils.parseDate(dateStr,
						new String[] { "yyyy-mm-dd HH:mm:ss" });
			}
			return duration(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String date(Date date, String format) {
		if (date == null) {
			return "";
		}

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date.getTime());
	}

	public static String duration1(Date date) {
		if (date == null) {
			return "";
		}

		Calendar before = Calendar.getInstance();
		before.setTime(date);

		Calendar today = Calendar.getInstance();
		long durationMillis = today.getTimeInMillis()
				- before.getTimeInMillis();

		StringBuffer sb = new StringBuffer();
		sb.append("(");

		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		if (durationMillis / DateUtils.MILLIS_PER_DAY > 0) {
			sb.append(sdf.format(date));
			sb.append(")");
			return sb.toString();
		}

		int hours = 0;
		if (durationMillis / DateUtils.MILLIS_PER_HOUR > 0) {
			hours = (int) (durationMillis / DateUtils.MILLIS_PER_HOUR);
			sb.append(hours);
			sb.append("小时前)");
			return sb.toString();
		}

		int minutes = 0;
		if (durationMillis / DateUtils.MILLIS_PER_MINUTE > 0) {
			minutes = (int) (durationMillis / DateUtils.MILLIS_PER_MINUTE);
			sb.append(minutes);
			sb.append("分钟前)");
			return sb.toString();
		}

		int seconds = 0;
		if (durationMillis / DateUtils.MILLIS_PER_SECOND > 0) {
			seconds = (int) (durationMillis / DateUtils.MILLIS_PER_SECOND);
			sb.append(seconds);
			sb.append("秒前)");
			return sb.toString();
		}

		return "1秒前";
	}

	/**
	 * 显示大小
	 *
	 * @param length
	 * @return String
	 */
	public static String printsize(Integer length) {

		int kb = 1024;
		int mb = 1024 * kb;

		double ret = length.doubleValue();

		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(2);

		if (length > mb) {
			ret = (double) length / mb;
			return nf.format(ret) + "MB";
		} else if (length > kb) {
			ret = (double) length / kb;
			return nf.format(ret) + "KB";
		}

		return length.toString();
	}

	/**
	 * 解析表情符号，将表情符号转换为图
	 *
	 * @param s
	 *            消息内容
	 * @return String
	 */

	public static String emotion(String s, Boolean wml) {

		if (s == null || "".equals(s)) {
			return "";
		}

		Config config = (Config) SpringHelper.getBean("test");

		String p = config.getImg() + "/images/faces/";
		List<Face> faces = config.getFaces();

		int size = faces.size();
		Face face = null;
		String img1 = " |[|img src=[|]";
		String img2 = "[|] alt=[|]表情[|] /|]| ";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < size; i++) {
			face = faces.get(i);
			if (s.indexOf(face.getFace()) < 0) {
				continue;
			}
			sb.append(img1);
			sb.append(p);
			sb.append(face.getFile());
			sb.append(img2);
			s = StringUtils.replace(s, face.getFace(), sb.toString());
			sb.delete(0, sb.capacity());
		}

		if (wml) {
			s = wml(s);
		}
		s = StringUtils.replace(s, "|[|", "<");
		s = StringUtils.replace(s, "|]|", ">");
		s = StringUtils.replace(s, "[|]", "\"");

		return s;
	}

	/**
	 * 连接字符串
	 *
	 * @param s1
	 * @param s2
	 * @return String
	 */
	public static String join(String s1, String s2) {
		return s1 + s2;
	}

	/**
	 * 截取字符串
	 *
	 * @param s
	 * @param num
	 * @return String
	 */
	public static String substring(String s, Integer num) {
		if (s == null || s.length() <= 0) {
			return "";
		}

		if (s.length() <= num - 3) {
			return s;
		}

		return Functions.txt(s.substring(0, num - 3) + "……");
	}

	/**
	 * 截取字符串,在后加…
	 *
	 * @param s
	 * @param num
	 * @return String
	 */
	public static String txtname(String s, Integer num) {

		if (s == null || s.length() <= 0) {
			return "";
		}

		if (s.startsWith("1")) {
			return Functions.txt(s);
		}

		if (s.length() <= num) {
			return s;
		}

		return Functions.txt(s.substring(0, num));
	}

	public static String[] split(String content, String sep) {
		if (content == null || sep == null) {
			return null;
		}
		return content.split(sep);
	}

	/**
	 * 根据手机号段获取所属省市信息
	 *
	 * @param mobile
	 *            手机号段（前七位）
	 * @return
	 */
	public static SysPhoneArea phoneAea(String mobile) {
		if (StringUtils.isEmpty(mobile)) {
			return null;
		}

		mobile = mobile.substring(0, 7);

		SysBiz biz = (SysBiz) SpringHelper.getBean("SysBiz");

		return biz.phoneAea(mobile);
	}

	/**
	 * 通过市ID获取城市信息
	 *
	 * @param city
	 * @return
	 */
	public static SysCity city(String cid) {
		if (StringUtils.isEmpty(cid)) {
			return null;
		}

		SysBiz biz = (SysBiz) SpringHelper.getBean("SysBiz");

		return biz.getCityById(cid);
	}

	/**
	 * 获取省份列表
	 *
	 * @param pid
	 * @return
	 */
	public static List<SysProvince> provinces() {
		SysBiz biz = (SysBiz) SpringHelper.getBean("SysBiz");

		return biz.getProvince();
	}

	/**
	 * 通过市ID获取城市信息
	 *
	 * @param pid
	 * @return
	 */
	public static SysProvince province(String pid) {
		if (StringUtils.isEmpty(pid)) {
			return null;
		}

		SysBiz biz = (SysBiz) SpringHelper.getBean("SysBiz");

		return biz.getProvinceById(pid);
	}

	/**
	 * 通过省ID获取城市列表
	 *
	 * @param city
	 * @return
	 */
	public static List<SysCity> getCityByPid(String pid) {
		if (StringUtils.isEmpty(pid)) {
			return null;
		}

		SysBiz biz = (SysBiz) SpringHelper.getBean("SysBiz");

		return biz.getCityByPid(pid);
	}
	/**
	 * 通过省ID获取城市列表
	 *
	 * @param city
	 * @return
	 */
	public static List<SysCity> getCities() {
		SysBiz biz = (SysBiz) SpringHelper.getBean("SysBiz");
		return biz.getCitys();
	}


	/**
	 * 限制外链
	 *
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static List<String> url() {
		SysBiz biz = (SysBiz) SpringHelper.getBean("SysBiz");

		return biz.url();
	}

	/**
	 * 处理文本中的链接与图片显示
	 *
	 * @param s
	 * @param vf
	 * @return
	 */
	public static String convert(String s, String vf) {
		if (s == null || s.trim().length() <= 0) {
			return s;
		}

		if (vf == null) {
			vf = "";
		}

		String convert = "";
		int u = s.indexOf("[/url]");
		String b = null;
		if (u > 0) {
			StringBuffer ss = new StringBuffer();
			while (u > 0) {
				b = s.substring(0, u + 6);
				ss.append(convert_url(b, vf));
				s = s.substring(u + 6);
				u = s.indexOf("[/url]");
			}

			ss.append(s);

			convert = ss.toString();
		} else {
			convert = s;
		}

		int img = convert.indexOf("[/img]");
		String c = null;
		if (img > 0) {
			StringBuffer ss = new StringBuffer();
			while (img > 0) {
				c = convert.substring(0, img + 6);
				ss.append(convert_img(c));
				convert = convert.substring(img + 6);
				img = convert.indexOf("[/img]");
			}
			ss.append(convert);
			return ss.toString();
		} else {
			return convert;
		}
	}

	/**
	 * 根据规则显示图片
	 *
	 * @param s
	 * @param vf
	 * @return
	 */
	public static String convert_img(String s) {
		if (StringUtils.isEmpty(s)) {
			return "";
		}

		Matcher m;
		m = com.uwyn.rife.tools.StringUtils.BBCODE_IMG.matcher(s);
		if (m.find()) {
			String a = m.group(1);
			if (!a.startsWith("http://")) {
				a = "http://" + a;
			}

			// int aa = a.indexOf("?");
			// String d = "";
			// if (aa > 0) {
			// d = a.substring(0, aa);
			// } else {
			// d = a;
			// }

			s = m.replaceAll("<img src=\"" + a + "\" alt=\".\" />");
		}

		return s;
	}

	/**
	 * 根据规则显示URL
	 *
	 * @param s
	 * @param vf
	 * @return
	 */
	public static String convert_url(String s, String vf) {
		if (StringUtils.isEmpty(s)) {
			return "";
		}

		Matcher m;
		m = com.uwyn.rife.tools.StringUtils.BBCODE_URL_SHORT.matcher(s);
		if (m.find()) {
			String a = m.group(1);
			if (!a.startsWith("http://")) {
				a = "http://" + a;
			}

			int aa = a.indexOf("?");
			String d = "";
			if (aa > 0) {
				d = a.substring(0, aa);
			} else {
				d = a;
			}

			int i = 0;
			List<String> url = url();
			if (url != null && url.size() > 0) {
				for (String c : url) {
					if (d.indexOf(c) > 0) {
						++i;
					}
				}
			}

			if (i == 0) {
				return s;
			}

			String b = null;
			int j = a.indexOf("|");
			if (j > 0) {
				b = a.substring(j + 1);
				a = a.substring(0, j);
			}

			if (a.indexOf(vf) < 0) {
				if (a.indexOf("?") > 0) {
					a += "&amp;vf=" + vf;
				} else {
					a += "?vf=" + vf;
				}
			}

			if (b == null || b.isEmpty()) {
				s = m.replaceAll("<a href=\"" + a + "\">$1</a>");
			} else {
				s = m.replaceAll("<a href=\"" + a + "\">" + b + "</a>");
			}
		}

		return s;
	}

	/**
	 * url转换
	 *
	 * @param url
	 * @return String
	 */
	public static String matched(String url) {
		if (url != null && url.contains("&")) {
			String bb = url.replaceAll("&", "&amp;");

			return bb;
		}

		return url;
	}

	/**
	 *
	 */
	public static String txt(String s) {
		if (s == null) {
			return "";
		}

		if ("".equals(s)) {
			return "";
		}

		s = s.replaceAll("&lt;", "<");
		s = s.replaceAll("&gt;", ">");
		s = s.replaceAll("&amp;", "&");
		s = s.replaceAll("&quot;", "\"");
		s = s.replaceAll("&quot;", "\"");
		s = s.replaceAll("&apos;", "\'");

		s = s.replaceAll("&nbsp;", " ");
		s = s.replaceAll("&ldquo;", "\"");
		s = s.replaceAll("&amp;ldquo;", "\"");
		s = s.replaceAll("&rdquo;", "\"");
		s = s.replaceAll("&amp;rdquo;", "\"");
		s = s.replaceAll("&mdash;", "__");
		s = s.replaceAll("&amp;mdash;", "__");
		s = s.replaceAll("&hellip;", "...");
		s = s.replaceAll("&amp;hellip;", "...");
		s = s.replaceAll("&middot;", ".");
		s = s.replaceAll("&amp;middot;", ".");

		s = s.replaceAll("<br/>|<br[\\s]+/>", "\n");

		int length = s.length();
		char[] ch = s.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; ++i) {
			if (ch[i] > 0xFFFD) {
				continue;
			}

			if (ch[i] < 0x20
					&& (ch[i] != '\t' && ch[i] != '\n' && ch[i] != '\r')) {
				continue;
			}

			switch (ch[i]) {
			case '<':
				sb.append("&lt;");
				break;
			case '>':
				sb.append("&gt;");
				break;
			case '&':
			case '$':
				sb.append("&amp;");
				break;
			case '"':
				sb.append("&quot;");
				break;
			case '\'':
				sb.append("&apos;");
				break;
			default:
				sb.append(ch[i]);
			}
		}

		String a = sb.toString();
		a = a.replaceAll("\\$\\$\\$", "\\$\\$");

		return a;
	}

	/**
	 * 去掉字符串中的转义符 \
	 *
	 * @param s
	 * @return
	 */
	public static String txt2(String s) {
		char[] ch = s.toCharArray();
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			char d;
			if (i >= ch.length - 1)
				d = c;
			else
				d = ch[i + 1];
			if (c == '\\' && (d == '\"' || d == '\'')) {
				continue;
			}
			builder.append(c);
		}
		return builder.toString();
	}

	public static String brieftext(String content, Integer len) {
		if (content == null) {
			return "";
		}

		String regEx = "<[/|\\s]*br[^>]*>";
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*blockquote[^>]*>((?!</?\\s*blockquote\\s*)(\r|\n|.))*<[/|\\s]*blockquote[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*blockquote[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*p[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*img[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		content = content.replaceAll("\n", "");

		if (content.length() <= len) {
			return content;
		}

		content = content.substring(0, len);
		return content;
	}

	/**
	 * html内容过滤
	 *
	 * @param content
	 * @return
	 */
	public static String clearscript(String content) {
		if (content == null) {
			return "";
		}

		String regEx = "<\\s*script[^>]*>((?!</?\\s*script\\s*)(\r|\n|.))*</\\s*script\\s*>";
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*script[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*div[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*li[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*ul[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*ol[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*table[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*td[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*th[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*tr[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*thead[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*form[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*input[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*fieldset[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*hr[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*tbody[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*tfoot[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*iframe[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*object[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*embed[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*br[\\s]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*span[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*a[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*em[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*strong[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*u[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		regEx = "<[/|\\s]*img[^>]*>";
		p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		m = p.matcher(content);
		content = m.replaceAll("");

		return content;
	}

	public static String wapcontent(String content) {
		if (content == null) {
			return "";
		}

		content = content.replaceAll("&nbsp;", " ");
		content = content.replaceAll("&ldquo;", "\"");
		content = content.replaceAll("&amp;ldquo;", "\"");
		content = content.replaceAll("&rdquo;", "\"");
		content = content.replaceAll("&amp;rdquo;", "\"");
		content = content.replaceAll("&mdash;", "__");
		content = content.replaceAll("&amp;mdash;", "__");
		content = content.replaceAll("&hellip;", "...");
		content = content.replaceAll("&amp;hellip;", "...");
		content = content.replaceAll("&middot;", ".");
		content = content.replaceAll("&amp;middot;", ".");
		content = content.replaceAll("&lt;", "<");
		content = content.replaceAll("&amp;lt;", "<");
		content = content.replaceAll("&gt;", ">");
		content = content.replaceAll("&amp;gt;", ">");

		String regEx = "<\\s*script[^>]*>((?!</?\\s*script\\s*)(\r|\n|.))*</\\s*script\\s*>";
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		content = m.replaceAll("");

		content = content.replaceAll("<br/>|<br[\\s]+/>", "\n");
		content = content.replaceAll("<.+?>", "");

		return content.trim();
	}

	/**
	 * 浏览数 modify by libin
	 */
	public static String browse(String view, String reply) {

		if (reply != null && reply.matches("\\d+") && view == null) {
			return reply;
		}

		if (view != null && view.matches("\\d+") && reply == null) {
			return view;
		}

		if (view == null && reply == null && !view.matches("\\d+")
				&& !reply.matches("\\d+")) {

			return "0";
		}

		int v = Integer.parseInt(view);
		int r = Integer.parseInt(reply);

		if (r > v) {
			return reply;
		} else {
			return view;
		}
	}

	/**
	 * 选取start - end的随机数
	 *
	 * @return String
	 */
	public static String rand(int start, int end) {
		Integer rand = start + (int) (Math.random() * (end - start));

		return rand.toString();
	}

	public static String halfShape(String txt) {
		if (txt == null || txt.equals("")) {
			return txt;
		}
		txt = txt.replaceAll("”", "\"");
		txt = txt.replaceAll("“", "\"");
		txt = txt.replaceAll("，", ",");
		txt = txt.replaceAll("；", ";");
		txt = txt.replaceAll("‘", "'");
		txt = txt.replaceAll("’", "'");
		txt = txt.replaceAll("＂", "\"");
		txt = txt.replaceAll("：", ":");
		txt = txt.replaceAll("＋", "+");
		txt = txt.replaceAll("＿", "__");
		txt = txt.replaceAll("）", ")");
		txt = txt.replaceAll("（", "(");
		txt = txt.replaceAll("＊", "*");
		txt = txt.replaceAll("＆", "&");
		txt = txt.replaceAll("＾", "^");
		txt = txt.replaceAll("％", "%");
		txt = txt.replace("＄", "$");
		txt = txt.replaceAll("＃", "#");
		txt = txt.replaceAll("＠", "@");
		txt = txt.replaceAll("！", "!");

		return txt;
	}

	/**
	 * 生成验证码KEY
	 *
	 * @return
	 */
	public static String vcode_key() {
		return ValidCode.setCodeByKey(4, 8);
	}

	/**
	 * 压缩时间算法
	 *
	 * @param time
	 * @return
	 */
	public static String compress(Long time) {
		if (time == 0l) {
			return Compress64.CompressNumber(System.currentTimeMillis(), 6);
		}

		return Compress64.CompressNumber(time, 6);
	}

	public static Integer UADetect(HttpServletRequest request) {
		if (request == null) {
			return 0;
		}
		String UA = request.getHeader("User-Agent");
		if (UA != null
				&& (UA.startsWith("Mozilla") || UA.startsWith("Opera") || UA
						.startsWith("MSIE"))) {
			return 1;
		}
		return 0;
	}
}
